#include <stdio.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <sys/un.h>  
#include<unistd.h>

char recv_buf[1024*1024*1024] = { 0 };

typedef struct head_s
{
    /* data */
    int index;
    int size;
    int checksum;
    unsigned char ptr[1920*1080*3];
};

typedef struct msg_head
{
    /* data */
    int head;
    int sum;
    int index;
    int length;
    int checksum;
};
 
#define CAN_SERVICE "CAN_SERVICE" 
int main(void)  
{ 
    int ret;  
    int socket_fd;   
    char snd_buf[1024];   
    static struct sockaddr_un srv_addr;  
 
// 创建socket 
    socket_fd=socket(PF_UNIX,SOCK_STREAM,0);  
    if(socket_fd<0)  
    {  
        perror("cannot create communication socket");  
        return 1;  
    }     
    srv_addr.sun_family=AF_UNIX;  
    strcpy(srv_addr.sun_path,CAN_SERVICE);  
 
// 连接到服务器  
    ret=connect(socket_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));  
    if(ret==-1)  
    {  
        perror("cannot connect to the server");  
        close(socket_fd);  
        return 1;  
    }  
    while (1)
    {
        /* code */
        // memset(snd_buf,0,1024);  
        // strcpy(snd_buf,"message from client");  
 
        // // 读取和写入    
        // write(socket_fd,snd_buf,sizeof(snd_buf)); 
        // usleep(1000000); 
        
        // memset(recv_buf,0,1024*1024*1024);  
        // int num=read(socket_fd,recv_buf,sizeof(recv_buf));  
        int num = recv(socket_fd, recv_buf, sizeof(recv_buf), 0);
        if(num <= 0)
        {
            printf("client close\n");
            return 0;
        }
        // printf("num:%d    ", num);
        if(num > 20)
        {    
            // for (size_t i = 0; i < 10; i++)
            // {
            //     printf(" %.2x", recv_buf[i]);
            //     /* code */
            // }
            msg_head *msg;
            msg = (msg_head *)recv_buf;
            printf("index:%d head:%.2x length:%d sum:%d \n", msg->index, msg->head, msg->length, msg->sum);
        }
        // printf(" \n", num);
        // head_s data = { 0 };
        // usleep(30000);

    }
    

    close(socket_fd);  
    return 0;  
} 